<template>
    <div>
        <Tables 
        :title ='title' 
        :butns = 'butns' 
        :field = 'field' 
        :tableData ='tableData' 
        :pageSize='pageSize'  
        :totalNum="total_num" 
        :collectField = "collect_field"
        :curPage ="cur_page" 
        :clearSelectItems="selectedItems"
        :rankable ="true"
        @reset="reset"  
        @loadData ="loadData" 
        @sentSelectedItems="getSelectedItems"
        @sentEditData="getEditData"
        @changePageSize = 'changePageSize'
        @rankData = "rankData"
        ></Tables>
        <Modals 
        :id ="'modal'" 
        :title ='title' 
        :field = 'field' 
        :collectField = "collect_field"
        :selectItem = "select_item" 
        :status = "status" 
        :errorMsg="errorMsg" 
        :type="type"
        :selectedItems="selectedItems" 
        :editData = "edit_data"  
        @sentErrorMsg = "getErrorMsg"
        @addEvent = 'addData'
        @delEvent = 'delData'
        @editEvent = 'editData'
        ></Modals>
    </div>
</template>
<script>
    import Tables from 'components/common/Tables'
    import Modals from 'components/common/Modals'
    import defendService from 'services/defendService' 
    import SystemService from 'services/systemService'
    import PageData from 'libs/PageData'

    export default {
		components:{
		    Tables,
            Modals
	    },
        data() {
            return{
                title:this.$t('safeplot.navBtn8'),
                butns:[
                    {
                        type:'add',
                        name:this.$t('common.addBtn'),
                        event:'addEvent',
                        icon:'/static/img/add.png',
                        class:'add_btn',
                    },
                    {
                        type:'del',
                        name:this.$t('common.delBtn'),
                        event:'delEvent',
                        icon:'/static/img/delete.png',
                        class:'del_btn',
                    },
                    {
                        type:'edit',
                        name:this.$t('common.editBtn'),
                        event:'editEvent',
                        icon:'/static/img/modpic.png',
                        class:'edit_btn',
                    }
                ],
                field: [
                    {
                        type:'checkbox',
                    },
                    {
                        type:'rank',
                    },
                    {
                        type:'text',
                        label:this.$t('safeplot.maskTableTh1'),
                        name:'name',          
                        regex:/^\w{1,9}$/,
                        requried:true, 
                        remark:this.$t('safeplot.remark1'),
                    
                    },
                    {
                        type:'ipinput',
                        label:this.$t('safeplot.maskTableTh2'),
                        name:'r_ip',
                        regex:/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/,
                        requried:true, 
                        remark:this.$t('safeplot.remark1')  
                    },
                    {
                        type:'text',
                        label:this.$t('safeplot.maskTableTh3'),
                        name:'v_ip',
                        regex:/^(!*(2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)(\/(3[0-2]|2[0-9]|1[0-9]|[1-9]))*(\-(!*(2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?))*(\/(3[0-2]|2[0-9]|1[0-9]|[1-9]))*$/,
                        requried:true, 
                        remark:this.$t('safeplot.maskRemark1')  
                    },
                    {
                        type:'text',
                        label:this.$t('safeplot.maskTableTh4'),
                        name:'vlan_id',
                        regex:/^\d$|^[1-9]\d{0,2}$|^[1-3]\d{3}$|^40\d[0-4]$/,
                        requried:true, 
                        remark:this.$t('safeplot.maskRemark2') 
                    },
                    {
                        type:'select',
                        label:this.$t('safeplot.maskTableTh5'),
                        name:'group_id',
                        emums:null,//下拉选项
                        regex:/\S+/,
                        requried:true, 
                        remark:this.$t('safeplot.remark1')  
                    }
                ],
                tableData:[],
                pageSize: [10, 20, 40, 80],
                size:10,
                total_num:1,
                cur_page:1,
                collect_field:'id',
                select_item:[],
                status:"",
                errorMsg:'',
                type:'',
                selectedItems:[],
                edit_data:{},
            }
        },
        created() {
            this.loadData()
            this.getLine()
        },
        methods:{
            getSelectedItems(selectedItems) {
                this.selectedItems = selectedItems
            },
             getErrorMsg(msg) {
                this.errorMsg = msg
            },
            getEditData(editdata) {
                this.edit_data = editdata 
            },
            reset(type) {
                this.status = ""
                this.errorMsg = ''
                this.type = type
                $('.inputs_wrap .error_foramt').removeClass('error_foramt')
            },  
            loadData(order = '', by = '') {
                let params = {
                    page: this.cur_page,
                    row: this.size,
                    order: order,
                    by: by
                }
                this.status = "ing"
                defendService.getSafePolt(7, params)
                .then((res) => {
					if (res.errcode === 0) {
                        this.status ='ok'
                        this.tableData =  res['7'].data.map((item)=>{
                             item.group_id = this.$t('common.tableRoad') + item.group_id
                             return item
                        })
                        this.total_num = res['7'].count
                    } else {
                        this.status ='error'
                        this.errorMsg = res.errorMessage	
                    }
				})
            },
            getLine() {
                SystemService.getLine()
                .then((res) => {
                    
                    if (res.errcode === 0) {
                        this.select_item = res.group_id.map((item)=>{
                            return  this.$t('common.tableRoad') + item
                        })
                    }
                })
            },
            changePageSize (nowpage, size) {
                this.cur_page = nowpage
                this.size = size
                this.loadData()
            }, 
            rankData (param) {
                this.loadData(param.order, param.by)
            },
            addData (data) {
                let sendStr = `${data.name}|${data.r_ip}|${data.v_ip}|${data.vlan_id}|${data.group_id.substr(2)}`
                this.status = "ing"
                defendService.addSafePolt(7, {7: sendStr})
                .then((res) =>{
                    if (res.errcode === 0) {
                        this.loadData()
                    }
                    this.status = "ok"
                })
            },
            editData (data) {
                let sendStr = `${data.id}|${data.name}|${data.r_ip}|${data.v_ip}|${data.vlan_id}|${data.group_id.substr(2)}`
                this.status = "ing"
                defendService.editSafePolt(7, {7: sendStr})
                .then((res) =>{
                    if (res.errcode === 0) {
                        this.loadData()
                    }
                    this.status = "ok"
                })
            },
            delData (data) {
                this.status = "ing"
                defendService.delSafePolt(7, {7: data.ids})
                .then((res) =>{
                    if (res.errcode === 0) {
                        this.loadData()
                        this.selectedItems = []
                    }
                    this.status = "ok"
                })
            }
        }
    }
</script>